package com.gitee.shop.service.impl;

import com.gitee.shop.domain.ItemCat;
import com.gitee.shop.mapper.ItemCatMapper;
import com.gitee.shop.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;
//    注入redis模版
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int deleteByPrimaryKey(Integer cid) {

        return 0;
    }

    @Override
    public int insert(ItemCat record) {
        return 0;
    }

    @Override
    public ItemCat selectByPrimaryKey(Integer cid) {

        return itemCatMapper.selectByPrimaryKey(cid);
    }

    @Override
    public List<ItemCat> selectAll() {
        return null;
    }

    @Override
    public int updateByPrimaryKey(ItemCat record) {
        return 0;
    }

    @Override
    public List<ItemCat> selectByStatus(int status) {
        List<ItemCat> itemCats = null;
        try{
            //先从缓存中取出分类的集合
            itemCats = (List<ItemCat>) redisTemplate.opsForList().index("itemCats", -1);

            //进行判断，如果取出来的集合为空
            if(itemCats == null || itemCats.isEmpty()){
                System.out.println("redis服务器中无数据！！！");
                //没有缓存，取出
                itemCats = itemCatMapper.selectByStatus(status);
                //再缓存
                redisTemplate.opsForList().leftPush("itemCats", itemCats);
            }
        }catch (Exception e){
            itemCats = itemCatMapper.selectByStatus(status);
            e.printStackTrace();
        }finally {
            return itemCats;
        }


    }
}
